package com.karaoke.android.kege.vm

import android.app.Fragment
import android.databinding.ObservableArrayList
import android.databinding.ObservableInt
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import com.karaoke.android.data.domain.DataDictionary
import com.karaoke.android.data.domain.QSong
import com.karaoke.android.kege.BR
import com.karaoke.android.kege.R
import com.karaoke.android.kege.fragment.KegeFragments
import com.karaoke.android.library.databind.ItemBinding
import com.karaoke.android.library.mvvm.FragmentViewModel
import universum.studios.android.fragment.manage.FragmentController
import universum.studios.android.fragment.manage.FragmentRequest
import kotlin.properties.Delegates

/**
 * Created by allen on 2017/4/22.
 */
class WordCountViewModel(private val f: Fragment) : FragmentViewModel(), AdapterView.OnItemClickListener
{
    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long)
    {
        if (selectedPosition.get() != position)
        {
            controller.newRequest(KegeFragments.SONG_PAGE).arguments(Bundle().apply {
                putInt("WordCount", words[position].id)
                putString("QueryType", QSong.QueryType.QUERY_WORD_COUNT.name)
            }).transaction(FragmentRequest.REPLACE)
                    .tag("song_page_${words[position].id}")
                    .execute()
        }
        selectedPosition.set(position)
    }

    val selectedPosition = ObservableInt().apply { set(-1) }
    private var controller by Delegates.notNull<FragmentController>()

    init
    {

        controller = FragmentController(f.childFragmentManager)
        controller.viewContainerId = R.id.sContainer
        controller.factory = KegeFragments()
    }

    override fun onInitLoad()
    {
        super.onInitLoad()
        words.add(DataDictionary().apply { id = 1;name = "一字";image = "{word-1}" })
        words.add(DataDictionary().apply { id = 2;name = "二字";image = "{word-2}" })
        words.add(DataDictionary().apply { id = 3;name = "三字";image = "{word-3}" })
        words.add(DataDictionary().apply { id = 4;name = "四字";image = "{word-4}" })
        words.add(DataDictionary().apply { id = 5;name = "五字";image = "{word-5}" })
        words.add(DataDictionary().apply { id = 6;name = "六字";image = "{word-6}" })
        words.add(DataDictionary().apply { id = 7;name = "七字";image = "{word-7}" })
        words.add(DataDictionary().apply { id = 8;name = "八字";image = "{word-8}" })
        words.add(DataDictionary().apply { id = 9;name = "更多";image = "{word-9}" })

        controller.newRequest(KegeFragments.SONG_PAGE).arguments(Bundle().apply {
            putInt("WordCount", words[0].id)
            putString("QueryType", QSong.QueryType.QUERY_WORD_COUNT.name)
        }).transaction(FragmentRequest.REPLACE)
                .tag("song_page_${words[0].id}")
                .execute()

    }

    val words = ObservableArrayList<DataDictionary>()
    val menuBindings: ItemBinding<DataDictionary> = ItemBinding.of<DataDictionary>(BR.item, R.layout.menu_list_item)
}